package com.sto.stoapp.activity.scanner.inquiry;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;

import com.sto.stoapp.adapter.InquirySelectAdapter;
import com.sto.stoapp.db.StoDataBaseOperation;
import com.sto.stoapp.db.StoTableName;
import com.sto.stoapp.manager.UnSentDataManager;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.util.UnifiedStaticType;

import java.util.HashMap;
import java.util.List;

import sto.android.app.StoPdaKeyEvent;

public class InquiryDetail {

	private String[] textMenu;
	private Context context;
	private InquiryFieldSql fieldSql;
	private StoTableName stoTableName;

	public InquiryDetail(Context context, InquiryFieldSql fieldSql,
			StoTableName stoTableName) {
		this.context = context;
		this.fieldSql = fieldSql;
		this.stoTableName = stoTableName;
	}

	/**
	 * 显示记录的详情
	 * 
	 * @param code
	 * @param item
	 * @param courierAdapter
	 * @param courierDatas
	 * @param delposition
	 */
	public void inquiryFunctionDetail(final String code, final int item,
			final InquirySelectAdapter courierAdapter,
			final List<HashMap<String, String>> courierDatas,
			final int delposition) {
		final String tablename = EnumTableNames.values()[item].getTableName();// 表名
		final String[] date = fieldSql.getInquiryField();// 日期
		final InquiryQueryTable querytable = new InquiryQueryTable(context);
		textMenu = querytable.getCodeInquiryDetail(tablename, code, date[0],
				item);
		if (textMenu == null)
			return;
		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
		builder.setTitle("记录详情").setIcon(android.R.drawable.ic_dialog_info)
				.setNegativeButton("取消", null).setItems(textMenu, null);
		String status = courierDatas.get(delposition).get(
				fieldSql.INQUIRYSTATUS);
		builder.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == StoPdaKeyEvent.KEYCODE_DEL
						&& event.getAction() == KeyEvent.ACTION_UP) {
					dialog.dismiss();
				}
				return false;
			}
		});
		if (status.equals(UnifiedStaticType.NO_STATUS)) {
			builder.setPositiveButton("删除",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							if (item == EnumTableNames.STATUS_TABLE_SALESMAN_ORDER_BACK
									.ordinal()) {
								Helper.showSoundToast(context, "订单打回不能被删除!",
										false);
								return;
							}
							courierDatas.remove(delposition);
							courierAdapter.notifyDataSetChanged();
							
							delBaggingAndSsendpiece(EnumTableNames.values()[item], code);
							
							delLoadCarSendPiece(EnumTableNames.values()[item], code);
							
							if (querytable.deleteInquiry(tablename, code,
									date[0])) {
								updateSetUnSendCount(1);
							}
						}
					});
		}
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
		// alertDialog.getWindow().getAttributes().x=0;
		// alertDialog.getWindow().getAttributes().y=24;
		// alertDialog.getWindow().setLayout(260, 300);
	}
	
	/**
     * 装袋发件删除
     */
    private void delBaggingAndSsendpiece(final EnumTableNames TABLE_STATUS, String code) {
        // 装袋&发件特殊处理
        if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_BAGGINGADNSENDPIECE) {
            // 统计袋号的个数
            int count = StoDataBaseOperation.getInstance(context)
                    .StatisticsBagNumberCount(TABLE_STATUS, code);
            if (count == 1) {
                String bagNumber = StoDataBaseOperation.getInstance(context)
                        .getBagNumber(TABLE_STATUS, code);
                StoDataBaseOperation.getInstance(context).deleteScanData(
                        EnumTableNames.STATUS_TABLE_SCANNING_BAGGING_OPERATE.ordinal(),
                        bagNumber);
                updateSetUnSendCount(1);
            }

        }
        // 中转-装袋&发件特殊处理
        else if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_CENTER_BAGGINGADNSENDPIECE) {
            // 统计袋号的个数
            int bag_count = StoDataBaseOperation.getInstance(context)
                    .StatisticsBagNumberCount(TABLE_STATUS, code);
            if (bag_count == 1) {
                String bagNumber = StoDataBaseOperation.getInstance(context)
                        .getBagNumber(TABLE_STATUS, code);
                StoDataBaseOperation.getInstance(context)
                        .deleteScanData(
                                EnumTableNames.STATUS_CENTER_TABLE_BAGGING_OPERATE.ordinal(),
                                bagNumber);
                updateSetUnSendCount(1);
            }

        }
        // 航空-扫描-装包&发件特殊处理
        else if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_SCAN_PACKET_SEND) {
            // 统计包号的个数
            int bag_count = StoDataBaseOperation.getInstance(context)
                    .StatisticsBagNumberCount(TABLE_STATUS, code);
            if (bag_count == 1) {
                String bagNumber = StoDataBaseOperation.getInstance(context)
                        .getBagNumber(TABLE_STATUS, code);
                // 删除包操作记录
                StoDataBaseOperation.getInstance(context).deleteScanData(
                        EnumTableNames.STATUS_TABLE_AVIATION_PACKAGE_OPERATE.ordinal(),
                        bagNumber);
                updateSetUnSendCount(1);

            }

        } else {
        }

    }
	
    /**
     * 装车发件删除
     * 
     * @author wxf
     * 
     * @param currCode
     */
    private void delLoadCarSendPiece(final EnumTableNames TABLE_STATUS, String currCode) {
        
        // 统计铅封号的个数
        int leadSealingcount = StoDataBaseOperation.getInstance(context)
                .StatisticsBagNumberCount(TABLE_STATUS, currCode);
        
        // 删除航空-扫描-装车发件记录
        if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_LOADCAR_SENDPIECE) {
            if (leadSealingcount == 1) {
                String leadSealingCode = StoDataBaseOperation.getInstance(context)
                        .getBagNumber(TABLE_STATUS, currCode);
                // 删除铅封操作记录
                StoDataBaseOperation.getInstance(context).deleteScanData(
                        EnumTableNames.STATUS_TABLE_AVIATION_LEAD_SEALING_OPERATE.ordinal(),
                        leadSealingCode);
                updateSetUnSendCount(1);
                
            }

        }
        // 删除-中转-装车发件记录
        if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_CENTER_LOADCAR_SENDPIECE) {
            if (leadSealingcount == 1) {
                String leadSealingCode = StoDataBaseOperation.getInstance(context)
                        .getBagNumber(TABLE_STATUS, currCode);
                // 删除铅封操作记录
                StoDataBaseOperation.getInstance(context).deleteScanData(
                        EnumTableNames.STATUS_TABLE_CENTER_LEAD_SEALING_OPERATE.ordinal(),
                        leadSealingCode);
                updateSetUnSendCount(1);
                
            }

        }
        // 删除-扫描员-装车发件记录
        if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_LOADCAR_SENDPIECE) {
            
            if (leadSealingcount == 1) {
                String leadSealingCode = StoDataBaseOperation.getInstance(context)
                        .getBagNumber(TABLE_STATUS, currCode);
                // 删除铅封操作记录
                StoDataBaseOperation.getInstance(context).deleteScanData(
                        EnumTableNames.STATUS_TABLE_SCANNING_LEAD_SEALING_OPERATE.ordinal(),
                        leadSealingCode);
                updateSetUnSendCount(1);
                
            }

        }
    }
	

	/**
	 * 更新未发送数据
	 */
	private void updateSetUnSendCount(int sendCount) {
		/*
		 * Intent intent = new Intent(BaseActivity.SEND_ACTION);
		 * intent.putExtra(BaseActivity.SEND_COUNT, sendCount);
		 * this.context.sendBroadcast(intent);
		 */
		// int unSend = UnSentDataManager.getInstance().getUnSent() - sendCount;
		// UnSentDataManager.getInstance().setUnSent(unSend);
		UnSentDataManager.getInstance().deleteCodeNumber(sendCount);
		System.out.println("updateSetUnSendCount");
	}

}
